package com.zb.store.controller;


import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.zb.code.service.ConCodeService;
import com.zb.entity.AccounLoginInfo;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.service.ZjStoreInboundService;
import com.zb.store.service.ConStoreInboundService;
import com.zb.utils.TokenUtil;
import com.zb.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * <p>
 * 入库表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2024-01-12
 */
@Api(tags = "仓库入库类接口")
@RestController
@RequestMapping("/store/inbound")
public class ZjStoreInboundController {
    @Resource
    private ConStoreInboundService conStoreInboundService;
    @Resource
    private ConCodeService conCodeService;

    @ApiOperation("获取未出入库的一物一码")
    @PostMapping("/getStoreCodeList")
    public Result getStoreCodeList(HttpServletRequest request, @RequestBody @Valid CodeInStoreListSearchVo searchVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取未出入库的一物一码成功").data(conCodeService.getStoreCodeList(custAccId, searchVo));
    }

    @ApiOperation("保存或保存并审核入库单")
    @PostMapping("/save/{submitType}")
    public Result save(HttpServletRequest request, @PathVariable @ApiParam(value = "提交类型：1-保存，2-保存并审核") String submitType,
                       @RequestBody @Valid StoreInboundVo inboundVo){
        Assert.isTrue(Pattern.matches("^[1|2]$", submitType), "提交类型参数不合法");
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        conStoreInboundService.save(loginInfo, submitType, null, inboundVo);
        return Result.success(Objects.equals("1", submitType)? "入库单保存成功" : "入库单保存并审核成功");
    }

    @ApiOperation("获取入库列表")
    @PostMapping("/getList")
    public Result getList(HttpServletRequest request, @RequestBody StoreOutboundListSelectVo selectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取入库列表成功").data(conStoreInboundService.getList(custAccId, selectVo));
    }

    @ApiOperation("根据入库单ID获取入库单详情")
    @GetMapping("/getInboundDetail/{id}")
    public Result getInboundDetail(@PathVariable @ApiParam(value = "入库单ID", required = true) Long id) {
        return Result.success("获取入库单详情成功").data(PageUtil.row(conStoreInboundService.getInboundDetail(id)));
    }

    @ApiOperation("根据入库单ID获取入库单详情物料")
    @GetMapping("/getInboundDetailMaterial/{id}")
    public Result getInboundDetailMaterial(@PathVariable @ApiParam(value = "入库单ID", required = true) Long id) {
        return Result.success("获取入库单详情物料成功").data(PageUtil.row(conStoreInboundService.getInboundDetailMaterial(id)));
    }

    @ApiOperation("确认入库")
    @PostMapping("/saveSureStoreOutbound/{id}")
    public Result saveSureInbound(HttpServletRequest request, @PathVariable @ApiParam(value = "出库单ID", required = true) Long id,
                                  @RequestBody @Valid StoreInboundLogisticsVo logisticsVo) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        conStoreInboundService.saveSureInbound(loginInfo, id, logisticsVo);
        return Result.success("入库成功");
    }

    @ApiOperation("作废入库单")
    @PostMapping("/cancelStoreInbound/{id}")
    public Result cancelStoreInbound(HttpServletRequest request, @PathVariable @ApiParam(value = "出库单ID", required = true) Long id,
                                     @RequestBody @Valid StoreRemarkVo storeVo) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        conStoreInboundService.cancelStoreInbound(loginInfo, id, storeVo.getRemark());
        return Result.success("作废入库单成功");
    }

    @ApiOperation("驳回入库单")
    @PostMapping("/rejectInbound/{id}")
    public Result rejectInbound(HttpServletRequest request, @PathVariable @ApiParam(value = "入库单ID", required = true) Long id,
                                 @RequestBody @Valid StoreRemarkVo storeVo) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        conStoreInboundService.rejectInbound(loginInfo, id, storeVo.getRemark());
        return Result.success("驳回入库单成功");
    }

}
